package com.hjqjl.whdemo.feature.feature0.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.whdemo.R;

import java.util.List;
import java.util.Map;

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    private final Context pActivity;
    private final List<String> mParentData;
    private final Map<String, List<String>> mChildDataMap;

    public MyExpandableListViewAdapter(Context context, List<String> parentData, Map<String, List<String>> childDataMap) {
        this.pActivity = context;
        this.mParentData = parentData;
        this.mChildDataMap = childDataMap;
    }

    @Override
    public int getGroupCount() {
        return mParentData.size();
    }

    // 获取当前父item下的子item的个数
    @Override
    public int getChildrenCount(int groupPosition) {
        String key = mParentData.get(groupPosition);
        return mChildDataMap.get(key).size();
    }

    // 获取当前父item的数据
    @Override
    public Object getGroup(int groupPosition) {
        return mParentData.get(groupPosition);
    }

    // 得到子item需要关联的数据
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key = mParentData.get(groupPosition);
        return (mChildDataMap.get(key).get(childPosition));
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // 得到子item的ID
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    //是否指定分组视图及其子视图的ID对应的后台数据改变也会保持该ID.
    @Override
    public boolean hasStableIds() {
        return true;
    }

    // 设置父item组件
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            LayoutInflater inflater = (LayoutInflater) pActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_my_expandable_listview_parent, parent, false);
//				convertView = View.inflate(context, R.layout.question_expandable_parent, null);//这种方法字体样式没变
            holder.parentTv = (TextView) convertView.findViewById(R.id.tv_myExpandableListView_item_parent);
            holder.parentImageViw = (ImageView) convertView.findViewById(R.id.iv_myExpandableListView_item_parent);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.parentTv.setText(mParentData.get(groupPosition));
        // 判断isExpanded就可以控制是按下还是关闭，同时更换图片
        if (isExpanded) {
            holder.parentImageViw.setBackgroundResource(R.drawable.ic_arrow_up);
        } else {
            holder.parentImageViw.setBackgroundResource(R.drawable.ic_arrow_down);
        }

        return convertView;
    }

    // 设置子item的组件
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        String key = mParentData.get(groupPosition);
        String info = mChildDataMap.get(key).get(childPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) pActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_my_expandable_listview_children, parent, false);
        }
        TextView tv = convertView.findViewById(R.id.tv_myExpandableListView_item_content);
        tv.setText(info);
        tv.setOnClickListener(v -> ToastUtils.showShort("" + info));
        return convertView;
    }

    //而isChildSelectable(int groupPosition, int childPosition)用来判断某Group某个child是否可可选。
    //我们可以添加条件控制某Group某个child可点或不可点击。当不加任何条件直接返回false,所有的组的child均不可点击。
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class ViewHolder {
        private TextView parentTv;
        private ImageView parentImageViw;
    }
}